<%@ page language="java" contentType="text/html; charset=utf-8" %>
<%@ page import="com.alibaba.fastjson.JSONArray" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="weaver.common.DateUtil" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="weaver.interfaces.cese.duss.ceseutil.CostUtil" %>
<%@ page import="weaver.interfaces.cese.duss.ceseutil.OcrUitl" %>
<%@ page import="java.util.Map" %>
<%@ page import="weaver.interfaces.cese.duss.ceseutil.ImageUtil" %>
<jsp:useBean id="bb" class="weaver.general.BaseBean"/>
<%
    JSONObject jsonData = new JSONObject();
    User user = HrmUserVarify.getUser( request, response );
    int userid = user.getUID();//人员
    Map<String, String> returnMap = OcrUitl.getDepartAndSub( userid + "" );
    String department = returnMap.get( "departmentid" );//部门
    String supsubcom = returnMap.get( "subcompanyid1" );//分部
    String currentDate = DateUtil.getCurrentDate();//当前日期
    //浪潮影像识别接口
    String YX_API_KEY = bb.getPropValue( "langchao", "YX_API_KEY" );
    String yx_url = bb.getPropValue( "langchao", "yx_url" );
    String yx_cut_url = bb.getPropValue( "langchao", "yx_cut_url" );
    String fileIds = Util.null2String( request.getParameter( "fileIds" ) );//文件ID
    String msg = "";//返回信息
    int sumCount = 0;//发票总数
    int failedCount = 0;//识别失败记数
    int successCount = 0;//识别成功记数
    int isRepCount = 0;//识别重复记数
    String[] arr = fileIds.split( "," );
    for (int i=0;i<arr.length;i++) {
        int oldFileId = Util.getIntValue(arr[i]);
        bb.writeLog( "--------------getImageRecognition_Ajax--------------oldFileId=" + oldFileId );
        //插入影像表
        boolean yxbz = OcrUitl.insertImageInfo( userid, oldFileId, currentDate );
        String newFileIds = ImageUtil.imageCut( userid, oldFileId, yx_cut_url, YX_API_KEY );
        newFileIds = "".equals(newFileIds)?oldFileId+"":newFileIds;
        String[] newArr = newFileIds.split( "," );
        for (int j=0;j<newArr.length;j++) {
            sumCount++;
            int fileId = Util.getIntValue(newArr[j]);
            bb.writeLog( "--------------getImageRecognition_Ajax--------------fileId" + fileId );
            String result = OcrUitl.sendPostHttpOCR( yx_url, fileId, YX_API_KEY );
            bb.writeLog( "发票识别接口返回：" + result );
            JSONObject resultJson = JSONObject.parseObject( result );
            //发票识别
            if (!"".equals( result )) {
                String formmodeid = CostUtil.getFormmodeId( "uf_fpxx" );
                JSONObject response1 = JSONObject.parseObject( resultJson.getString( "response" ) );
                JSONObject data = JSONObject.parseObject( response1.getString( "data" ) );
                JSONArray identifyResults = JSONArray.parseArray( data.getString( "identifyResults" ) );
                for (int k = 0; k < identifyResults.size(); k++) {
                    JSONObject results = (JSONObject) identifyResults.get( k);
                    String category = results.getString( "category" );//发票类型
                    JSONObject details = JSONObject.parseObject( results.getString( "details" ) );//发票识别的具体信息
                    bb.writeLog( "=================category=" + category );
                    boolean flag;
                    switch (category) {
                        case "vat":
                            flag = OcrUitl.getTicketType_var( userid, currentDate, supsubcom, department, fileId, formmodeid, category, details );
                            if (flag) {
                                successCount++;
                            }else {
                                isRepCount++;
                            }
                            if (yxbz) {
                                OcrUitl.updateImageInfo( userid, fileId, currentDate );
                            }
                            break;
                        case "vat_roll":
                            flag = OcrUitl.getTicketType_var( userid, currentDate, supsubcom, department, fileId, formmodeid, category, details );
                            if (flag) {
                                successCount++;
                            }else {
                                isRepCount++;
                            }
                            if (yxbz) {
                                OcrUitl.updateImageInfo( userid, fileId, currentDate );
                            }
                            break;
                        case "machine_invoice":
                            flag = OcrUitl.getTicketType_machine_invoice( userid, currentDate, supsubcom, department, fileId, formmodeid, category, details );
                            if (flag) {
                                successCount++;
                            }else {
                                isRepCount++;
                            }
                            if (yxbz) {
                                OcrUitl.updateImageInfo( userid, fileId, currentDate );
                            }
                            break;
                        case "vehicle_invoice":
                            flag = OcrUitl.getTicketType_vehicle_invoice( userid, currentDate, supsubcom, department, fileId, formmodeid, category, details );
                            if (flag) {
                                successCount++;
                            }else {
                                isRepCount++;
                            }
                            if (yxbz) {
                                OcrUitl.updateImageInfo( userid, fileId, currentDate );
                            }
                            break;
                        case "air_ticket":
                            flag = OcrUitl.getTicketType_air_ticket( userid, currentDate, supsubcom, department, fileId, formmodeid, category, details );
                            if (flag) {
                                successCount++;
                            }else {
                                isRepCount++;
                            }
                            if (yxbz) {
                                OcrUitl.updateImageInfo( userid, fileId, currentDate );
                            }
                            break;
                        case "taxi_ticket":
                            flag = OcrUitl.getTicketType_taxi_ticket( userid, currentDate, supsubcom, department, fileId, formmodeid, category, details );
                            if (flag) {
                                successCount++;
                            }else {
                                isRepCount++;
                            }
                            if (yxbz) {
                                OcrUitl.updateImageInfo( userid, fileId, currentDate );
                            }
                            break;
                        case "train_ticket":
                            flag = OcrUitl.getTicketType_train_ticket( userid, currentDate, supsubcom, department, fileId, formmodeid, category, details );
                            if (flag) {
                                successCount++;
                            }else {
                                isRepCount++;
                            }
                            if (yxbz) {
                                OcrUitl.updateImageInfo( userid, fileId, currentDate );
                            }
                            break;
                        case "pass_ticket":
                            flag = OcrUitl.getTicketType_pass_ticket( userid, currentDate, supsubcom, department, fileId, formmodeid, category, details );
                            if (flag) {
                                successCount++;
                            }else {
                                isRepCount++;
                            }
                            if (yxbz) {
                                OcrUitl.updateImageInfo( userid, fileId, currentDate );
                            }
                            break;
                        case "bus_ticket":
                            flag = OcrUitl.getTicketType_bus_ticket( userid, currentDate, supsubcom, department, fileId, formmodeid, category, details );
                            if (flag) {
                                successCount++;
                            }else {
                                isRepCount++;
                            }
                            if (yxbz) {
                                OcrUitl.updateImageInfo( userid, fileId, currentDate );
                            }
                            break;
                        case "small_ticket":
                            flag = OcrUitl.getTicketType_small_ticket( userid, currentDate, supsubcom, department, fileId, formmodeid, category, details );
                            if (flag) {
                                successCount++;
                            }else {
                                isRepCount++;
                            }
                            if (yxbz) {
                                OcrUitl.updateImageInfo( userid, fileId, currentDate );
                            }
                            break;
                        case "quota_invoice":
                            flag = OcrUitl.getTicketType_quota_invoice( userid, currentDate, supsubcom, department, fileId, formmodeid, category, details );
                            if (flag) {
                                successCount++;
                            }else {
                                isRepCount++;
                            }
                            if (yxbz) {
                                OcrUitl.updateImageInfo( userid, fileId, currentDate );
                            }
                            break;
                        case "others":
                            flag = OcrUitl.getTicketType_others( userid, currentDate, supsubcom, department, fileId, formmodeid, category, details );
                            if (flag) {
                                successCount++;
                            }else {
                                isRepCount++;
                            }
                            if (yxbz) {
                                OcrUitl.updateImageInfo( userid, fileId, currentDate );
                            }
                            break;
                    }
                }
            } else {
                failedCount++;
            }
        }
    }
    msg =  "发票识别结果：[识别总数:" + sumCount + "张]；[识别成功:" + successCount + "张]；" +
            "[识别失败:" + failedCount + "张]；[识别重复:" + isRepCount + "张]";
    jsonData.put( "msg", msg );
    out.print( jsonData.toJSONString() );
%>


